import moment from 'moment';

export default {
  install: function (Vue, options) {
      Vue.prototype.serialize2Obj = function (data) {
        var obj = {};
        $.each(data,function(i,v){
          obj[v.name] = v.value;
        })
        return obj;
      },
      //保留两位小数
      Vue.prototype.toDecimal2NoZero = function (x) {
        var f = Math.round(x * 100) / 100;
        var s = f.toString();
        return s;
      },
      Vue.prototype.toFix2 = function(num) {
        return Math.round(num * 100) / 100;
      },
      Vue.prototype.toFix3 = function(num) {
        return Math.round(num * 1000) / 1000;
      },
      Vue.prototype.toFix4 = function(num) {
        return Math.round(num * 10000) / 10000;
      },
      Vue.prototype.toFix6 = function(num) {
        return Math.round(num * 1000000) / 1000000;
      },
      Vue.prototype.checkNull = function (val) {
        return val != '' && val != undefined && val.length != '';
      },
      Vue.prototype.checkObjNull = function (val) {
        return val != undefined ? val : '';
      },
      Vue.prototype.toFloat = function (val) {
        return !val ? 0 : parseFloat(val);
      },
      // 限制只能输入数字(可以输入两位小数点)
      Vue.prototype.limitInputPointNumber = function (val) {
        if (val === 0 || val === "0" || val === "" || val === undefined) {
          return "";
        } else {
          let value = null;
          value = val.replace(/[^\d.]/g, ""); // 清除“数字”和“.”以外的字符
          value = value.replace(/\.{2,}/g, "."); // 只保留第一个. 清除多余的
          value = value
            .replace(".", "$#$")
            .replace(/\./g, "")
            .replace("$#$", ".");
          value = value.replace(/^(-)*(\d+)\.(\d\d).*$/, "$1$2.$3"); // 只能输入两个小数
          return value;
        }
    },
    //过滤null并加单位
    Vue.prototype.attachUnit = function(attr, unit){
      return attr ? attr + unit : '';
    },
    //过滤null并加单位，默认为0
    Vue.prototype.attachUnit2 = function(attr, unit){
      return attr ? attr + unit : 0 + unit;
    },
    //过滤变量为null报错
    Vue.prototype.filterNull = function(obj){
      return obj != null ? obj : {};
    },
    //过滤变量为null报错
    Vue.prototype.goTo = function(path, params) {
      let routeData = this.$router.resolve({
        path: path,
        query: params
      });
      window.open(routeData.href, '_blank');
    },
    Vue.prototype.showFile = function(path) {
      let url = `http://${location.host}/prod-api${path}`;
      window.open(url);
    },
    Vue.prototype.packFileUrl = function(path) {
      return `http://${location.host}/prod-api${path}`;
    },
    Vue.prototype.getMonthFirDate = function() {
      return moment().startOf('month').format('YYYY-MM-DD');
    }
  }
}
